using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace Game.UI {
	public sealed class ResizeHandler : MonoBehaviour, IDragHandler {
		public Vector2 AxisFilter = Vector2.one;

		UIWindow _owner;

		private void Awake() {
			_owner = GetComponentInParent<UIWindow>();
		}

		public void OnDrag(PointerEventData eventData) {
			if ( !_owner.Resizable ) {
				return;
			}
			var delta = new Vector2(-eventData.delta.x, -eventData.delta.y) * AxisFilter;
			var newSize =_owner.RectTransform.sizeDelta + delta;
			_owner.RectTransform.sizeDelta = new Vector2(Mathf.Max(_owner.MinSize.x, newSize.x), Mathf.Max(_owner.MinSize.y, newSize.y));
			_owner.RectTransform.anchoredPosition -= delta * AxisFilter * 0.5f * new Vector2(_owner.RectTransform.sizeDelta.x > _owner.MinSize.x ? 1 : 0, _owner.RectTransform.sizeDelta.y > _owner.MinSize.y ? 1 : 0);

			//_owner.RectTransform.pivot = OverridePivot;
		}
	}

}
